package zsc.nine.onlinemall.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zsc.nine.onlinemall.entity.Order;
import zsc.nine.onlinemall.service.OrderService;

import java.util.List;

/**
 * @ClassName: OrderController
 * @Description: Controller of Order operation
 * @Author: Zainus
 * @CreateDate: Jul.23, 2021
 * @UpdateDate: Jul.23, 2021
 * @Version: [v1.0]
 */

/**
 * @类名: MailController
 * @描述: 订单管理控制类接口
 * @创建人: 叶增钥
 * @创建时间: 2021年7月23日
 * @更新时间: 2021年7月23日
 * @版本: [v1.0]
 */
@RequestMapping("/Order")
@RestController
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     *
     * @param Sum
     * @param Receiver
     * @param Address
     * @param Mobile
     * @param ItemNumber
     * @param ProductId
     * @param CreatorEmail
     * @return
     */
    @PostMapping("/Create")
    public String OrderCreate(@RequestParam(value = "Sum", required = false) String Sum,
                              @RequestParam(value = "Receiver", required = false) String Receiver,
                              @RequestParam(value = "Address", required = false) String Address,
                              @RequestParam(value = "Mobile", required = false) String Mobile,
                              @RequestParam(value = "ItemNumber", required = false) String ItemNumber,
                              @RequestParam(value = "ProductId", required = false) String ProductId,
                              @RequestParam(value = "CreatorEmail", required = false) String CreatorEmail,
                              @RequestParam(value = "ProductName", required = false) String productName) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("OrderCreateResult", "Fail");

        if (Sum == null
                || Receiver == null
                || Address == null
                || Mobile == null
                || ItemNumber == null
                || ProductId == null
                || CreatorEmail == null) {
            jsonObject.put("OrderCreateResult", "Parameter-Null");
        } else if (Sum.trim().isEmpty()
                || Receiver.trim().isEmpty()
                || Address.trim().isEmpty()
                || Mobile.trim().isEmpty()
                || ItemNumber.trim().isEmpty()
                || ProductId.trim().isEmpty()
                || CreatorEmail.trim().isEmpty()) {

            jsonObject.put("OrderCreateResult", "Empty");

        } else if (orderService.createOrder(Sum, Receiver, Address, Mobile, ItemNumber, ProductId, CreatorEmail)) {
            jsonObject.put("OrderCreateResult", "Success");
            jsonObject.put("OrderCreateIdResult", orderService.getLength());

        }


        return jsonObject.toJSONString();
    }

    /**
     *
     * @param OrderId
     * @return
     */
    @PostMapping("/Deliver")
    public String OrderDeliver(@RequestParam(value = "OrderId") String OrderId) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("OrderDeliverResult", "Fail");

        if (OrderId == null) {
            jsonObject.put("OrderDeliverResult", "Parameter-Null");
        } else if (OrderId.trim().isEmpty()) {
            jsonObject.put("OrderDeliverResult", "Empty");
        } else {
            try {
                Thread.sleep(5000);
                System.out.println("将在5s后发货完毕");
                if (orderService.deliver(OrderId)) {
                    jsonObject.put("OrderDeliverResult", "Success");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        return jsonObject.toJSONString();
    }

    /**
     *
     * @param OrderId
     * @return
     */
    @PostMapping("/Refund")
    public String OrderRefund(@RequestParam(value = "OrderId") String OrderId) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("OrderRefundResult", "Fail");

        if (OrderId == null) {
            jsonObject.put("OrderRefundResult", "Parameter-Null");
        } else if (OrderId.trim().isEmpty()) {
            jsonObject.put("OrderRefundResult", "Empty");
        } else {
            try {
                Thread.sleep(5000);
                System.out.println("将在5s后退货完毕");
                if (orderService.refund(OrderId)) {
                    jsonObject.put("OrderRefundResult", "Success");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        return jsonObject.toJSONString();
    }

    /**
     *
     * @param OrderId
     * @return
     */
    @PostMapping("/Get")
    public String OrderGet(@RequestParam(value = "OrderId") String OrderId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("OrderGetResult", "Fail");

        if (OrderId == null) {
            jsonObject.put("OrderGetResult", "Parameter-Null");
        } else if (OrderId.trim().isEmpty()) {
            jsonObject.put("OrderGetResult", "Empty");
        } else if (!orderService.orderExist(OrderId)) {
            jsonObject.put("OrderGetResult", "Wrong-Id");
        } else {
            Order order = orderService.findOrderById(OrderId);
            if (order != null) {
                jsonObject.put("OrderGetResult", "Success");
                jsonObject.put("OrderGetObjectResult", order);
            }
        }
        return jsonObject.toJSONString();
    }

    /**
     *
     * @param OrderId
     * @return
     */
    //删除订单
    @PostMapping("/Delete")
    public String OrderDelete(@RequestParam(value = "OrderId") String OrderId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("OrderDeleteResult", "Fail");

        if (OrderId == null) {
            jsonObject.put("OrderDeleteResult", "Parameter-Null");
        } else if (OrderId.trim().isEmpty()) {
            jsonObject.put("OrderDeleteResult", "Empty");
        } else if (!orderService.orderExist(OrderId)) {
            jsonObject.put("OrderDeleteResult", "Wrong-Id");
        } else if (orderService.deleteOrderById(OrderId)) {
            jsonObject.put("OrderDeleteResult", "Success");
        }

        return jsonObject.toJSONString();
    }

    /**
     *
     * @param Index
     * @param Size
     * @return
     */
    @PostMapping("/GetList")
    public String OrderGetList(@RequestParam(value = "Index", required = false) String Index,
                               @RequestParam(value = "Size", required = false) String Size) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("OrderGetListResult", "Fail");

        /*后端校验数据合法性*/
        if (Index == null
                || Size == null) {

            jsonObject.put("OrderGetListResult", "Parameter-Null");

        } else if (Index.trim().isEmpty()
                || Size.trim().isEmpty()) {
            jsonObject.put("OrderGetListResult", "Empty");
        } else {

            jsonObject.put("OrderGetListResult", "Success");
            jsonObject.put("OrderGetListObjectResult", orderService.pagedFindById(Index, Size));

        }

        return jsonObject.toJSONString();

    }

    /**
     *
     * @param Index
     * @param Size
     * @param CreatorEmail
     * @return
     */

    @PostMapping("/GetListByCreatorEmail")
    public String OrderGetListByCreatorEmail(@RequestParam(value = "Index", required = false) String Index,
                                          @RequestParam(value = "Size", required = false) String Size,
                                          @RequestParam(value = "CreatorEmail", required = false) String CreatorEmail) {


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("OrderGetListByCreatorEmailResult", "Fail");

        /*后端校验数据合法性*/
        if (Index == null
                || Size == null
                || CreatorEmail == null) {

            jsonObject.put("OrderGetListByCreatorEmailResult", "Parameter-Null");

        } else if (Index.trim().isEmpty()
                || Size.trim().isEmpty()
                || CreatorEmail.trim().isEmpty()) {
            jsonObject.put("OrderGetListByCreatorEmailResult", "Empty");
        } else {

            jsonObject.put("OrderGetListByCreatorEmailResult", "Success");
            jsonObject.put("OrderGetListByCreatorEmailObjectResult", orderService.pagedFindByCreatorEmail(Index, Size, CreatorEmail));

        }

        return jsonObject.toJSONString();

    }

    /**
     *
     * @return
     */
    @PostMapping("/GetListSize")
    public String OrderGetListSize() {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("OrderGetListSizeResult", orderService.getLength());

        return jsonObject.toJSONString();

    }
    @PostMapping("/FindAll")
    public String FindAll(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("OrderObjectResult","Fail");

        List<Order> orderList = orderService.findAll();

        jsonObject.put("OrderObjectResult",orderList);
        return jsonObject.toJSONString();
    }


}
